package com.fand.stater.context;

import java.util.Map;
import java.util.Objects;

import cn.hutool.core.map.MapUtil;

/**
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/6/26
 * Version: 1.0
 */
public class GlobalContextThreadLocal {

    private static ThreadLocal<Map<String,Object>> threadLocalData = new ThreadLocal<>();

    public static <V> void set(String key,V data){
        Map<String, Object> mapData = threadLocalData.get();
        if(Objects.isNull(mapData)){
            mapData = MapUtil.newHashMap();
            mapData.put(key,data);
            threadLocalData.set(mapData);
            return;
        }
        mapData.put(key,data);
    }

    public static <V> V get(String key){
        Map<String, Object> mapData = threadLocalData.get();
        if(Objects.isNull(mapData)){
            return null;
        }
        return (V) mapData.get(key);
    }

    public static void remove(){
        threadLocalData.remove();
    }
}
